 -*- coding: utf-8 -*-
import pickle

class xChamp(object):
    
    def __init__(self,args):
        self.Nom = None
        self.Type = None
        self.Taille = None
        self.Precision = None
        self.ListeContraintes = None
        if ('nom' in args.keys()):
            self.Nom=args['nom']
        if ('type' in args.keys()):
            self.Type=args['type']
        if ('taille' in args.keys()):
            self.Taille=args['taille']
        if ('precision' in args.keys()):
            self.Precision=args['precision']
        if ('listecontraintes' in args.keys()):
            self.ListeContraintes=args['listecontraintes']
        pass
    
class xTable(object):
    def __init__(self,args):
        self.Nom=None
        self.User=None
        self.ListeChamps=[]
        if ('user' in args.keys()):
            self.User=args['user']
        if ('nom' in args.keys()):
            self.Nom=args['nom']
        if ('listechamps' in args.keys()):
            self.ListeChamps=args['listechamps']
        pass

class xTableauEnregistrement(object):
    def __init__(self,args):
        self.ListeChamps = None
        self.ListeEnregistrement = None
        self.NomTable = None
        if ('listeenregistrement' in args.keys()):
            self.ListeEnregistrement=args['listeenregistrement']
        if ('listenomchamps' in args.keys()):
            self.ListeNomChamps=args['listenomchamps']
        if ('nomtable' in args.keys()):
            self.NomTable=args['nomtable']
            
class InterfaceBaseDonnees(object):
    def __init__(self, Service, Communication):
        self.Service = Service
        self.Communication = Communication

    def RecevoirVue(self,NomVue):
        f=open('Vues/'+NomVue+'.py','r')
        p=f.readlines()
        f.close()
        return p
        pass
    
    def RecevoirInterfaceBD(self):
        f=open('BD_InterfaceBaseDonnees.py','r')
        p=f.readlines()
        f.close()
        return p
        pass
    def Authentification(self,nom,pwd):
        return self.Communication.Authentification(nom,pwd)
        pass
    
    #paramètre : nom de la table
    #retour : xTable
    def ObtenirTable(self):
        if self.Service == "Client" :
            return pickle.loads(self.Communication.ObtenirTable())
        elif self.Service == "Serveur" :
            return pickle.dumps(self.Communication.ObtenirTable())
    
    #paramètre : nom de la table
    #retour : liste de xChamps
    def ObtenirChamp(self, NomTable):
        if self.Service == "Client":
            return pickle.loads(self.Communication.ObtenirChamp(pickle.dumps(NomTable)))
        elif self.Service == "Serveur":
            return pickle.dumps(self.Communication.ObtenirChamp(pickle.loads(NomTable)))

    #paramètre : nom de la table
    def EffacerTable(self, NomTable):
        pass

    
    def EntrerEnregistrement(self, LaTable):
        if self.Service == "Client":
            if not isinstance(LaTable, Table):
                return [0, "Envoyer comme argument un objet Table"]
            else:
               return pickle.loads(self.Communication.EntrerEnregistrement(pickle.dumps(LaTable)))
        elif self.Service == "Serveur" :
                return pickle.dumps(self.Communication.EntrerEnregistrement(pickle.loads(LaTable)))

    def Commit(self):
      return self.Communication.Commit()
    
    def CreerTable(self, LaTable):
        if self.Service == "Client":
               return pickle.loads(self.Communication.CreerTable(pickle.dumps(LaTable)))
        elif self.Service == "Serveur" :
                return pickle.dumps(self.Communication.CreerTable(pickle.loads(LaTable)))
    
    def ObtenirEnregistrement(self, NomsChamps, NomTable):
        if self.Service == "Client":
           return pickle.loads(self.Communication.ObtenirEnregistrement(pickle.dumps( NomsChamps),pickle.dumps( NomTable)))
        elif self.Service == "Serveur" :
           return pickle.dumps(self.Communication.ObtenirEnregistrement(pickle.loads( NomsChamps),pickle.loads( NomTable)))   
    
    def ModifierChamps(self, LaTable):
        if self.Service == "Client":
               return pickle.loads(self.Communication.ModifierChamps(pickle.dumps(LaTable)))
        elif self.Service == "Serveur" :
                return pickle.dumps(self.Communication.ModifierChamps(pickle.loads(LaTable)))

    def AjouterChamps(self, LaTable):
        if self.Service == "Client":
               return pickle.loads(self.Communication.AjouterChamps(pickle.dumps(LaTable)))
        elif self.Service == "Serveur" :
                return pickle.dumps(self.Communication.AjouterChamps(pickle.loads(LaTable)))

    def SupprimerChamps(self, LaTable):
        if self.Service == "Client":
               return pickle.loads(self.Communication.SupprimerChamps(pickle.dumps(LaTable)))
        elif self.Service == "Serveur" :
                return pickle.dumps(self.Communication.SupprimerChamps(pickle.loads(LaTable)))
